//
//  GetPhotosWithPhotosSetIDViewController.m
//  FlickrSearching
//
//  Created by Dat on 4/26/15.
//  Copyright (c) 2015 HOCVIEN. All rights reserved.
//

#import "GetPhotosWithPhotosSetIDViewController.h"
#import "GetPhotosWithPhotosSetIDTableViewCell.h"

@interface GetPhotosWithPhotosSetIDViewController ()
@property (nonatomic, strong) NSMutableArray *myArray;
@property (nonatomic, strong) NSMutableArray *myFilter;
@property (nonatomic) NSInteger total;

//@property (nonatomic, retain) BOOL *isFitered;
@end

@implementation GetPhotosWithPhotosSetIDViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableview.delegate = self;
    self.myTableview.dataSource = self;
    self.mySearchBar.delegate = self;
}
-(void)viewWillAppear:(BOOL)animated{
    
    NSString *urlString = @"https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=b5d694a91e3d14e972db299d827bdaf4&user_id=91008793%40N00&primary_photo_extras=url_t&format=json&nojsoncallback=1&api_sig=5041a4369c88935c8b5b46c26502d024";
    NSURL *url = [[NSURL alloc]initWithString:urlString];
    NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:30];
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (!connectionError) {
            //NSLog(@"data=====%@",data);
            //NSLog(@"respone======%@",response);
            
            NSError *error;
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (!error) {
                //NSLog(@"dictionary=====%@",dictionary);
                NSDictionary *photoDictionary = [dictionary objectForKey:@"photosets"];
                NSInteger totalimages = [[photoDictionary objectForKey:@"total"] integerValue];
                
                self.total = totalimages;
                //NSLog(@"photodictionary====%@",photoDictionary);
//                int total = [photoDictionary[@"total"]intValue];
                //NSLog(@"total======%d",total);
                NSArray *photoArrays = [photoDictionary objectForKey:@"photoset"];
                self.myArray = [NSMutableArray arrayWithArray:photoArrays];
//                self.myArray = photoDictionary;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.myTableview reloadData];
                });
            }
            
        }else{
            //NSLog (@"ConnectionError  ===%@",connectionError);
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    GetPhotosWithPhotosSetIDTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    NSDictionary *photos = self.myArray;
//    int total = photos[@"total"];
//    NSArray *photoArr = [photos objectForKey:@"photo"];
//    NSDictionary *items1 = self.myArray;
    
    NSDictionary *items = self.myArray [indexPath.row];
    NSDictionary *title = [items objectForKey:@"title"];
    NSDictionary *images = [items objectForKey:@"primary_photo_extras"];
    //NSDictionary *totalphotos = [items objectForKey:@"photos"];
    if (!Cell) {
        [tableView registerClass:[GetPhotosWithPhotosSetIDTableViewCell class] forCellReuseIdentifier:identifier];
        Cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    }
    
    Cell.myLable1.text = [title objectForKey:@"_content"];
    Cell.myLable2.text = [NSString stringWithFormat:@"%@", [items objectForKey:@"photos"]];
    
    Cell.myImage.image = nil;
    Cell.imgLink = images[@"url_t"];
                    
    NSString *urlString = [images objectForKey:@"url_t"];
    dispatch_async(dispatch_queue_create("download", 0), ^{
        NSURL *url_t = [[NSURL alloc]initWithString:urlString];
        NSData *data = [[NSData alloc]initWithContentsOfURL:url_t];
        UIImage *images = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([Cell.imgLink isEqualToString:urlString]) {
                Cell.myImage.image = images;
            }
            
        });
    });
    NSLog(@"====%ld",indexPath.row);
    
    return Cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.total;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


@end
